package com.sjcq.demoexample.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sjcq.demoexample.entity.SysLog;
import com.sjcq.demoexample.entity.User;
import com.sjcq.demoexample.mapper.SysLogMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;

@Controller
public class LogController {
    @Autowired
    private SysLogMapper sysLogMapper;

    @RequiresPermissions("per:admin")
    @RequestMapping("logList")
    public String logList(Model model){
        User o = (User) SecurityUtils.getSubject().getPrincipal();
        List<SysLog> list = sysLogMapper.findAllList();
        model.addAttribute("list", list);
        return "logList";
    }

    @RequiresPermissions("per:admin")
    @RequestMapping("delete/{id}")
    public String delete(@PathVariable Long id){
        try {
            sysLogMapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
        return "redirect:/logList";
    }


    @ResponseBody
    @RequestMapping("test/{page}")
    public  PageInfo<SysLog> test(@PathVariable String page){
        int pageSize = Integer.parseInt(page);
        // 使用分页
        try {
            PageHelper.startPage(1, pageSize);
            // 根据分页信息查询所有
            List<SysLog> allList = sysLogMapper.findAllList();
            // 封装到PageInfo里面,这里由框架自动配置的类
            PageInfo<SysLog> pageInfo = new PageInfo<>(allList);
            return pageInfo;
        } catch (Exception e) {
            e.printStackTrace();
        }
            return null;
    }
}
